package com.hc.sort.manager;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import com.hc.sort.server.dao.*;
import com.hc.sort.server.pojo.BusinessType;
import com.hc.sort.server.service.BusinessTypeService;
import com.hc.sort.server.service.BusinessTypeServiceImpl;
import java.awt.event.*;
import java.util.List;

public class BusinessTypeAlterDialog extends JDialog {
	
	//持有父窗口的实例，在保存成功的时候刷新表格数据
	ManagerUI managerUI;
	//业务层对象
	BusinessTypeService businessTypeService=new BusinessTypeServiceImpl();

	private final JPanel contentPanel = new JPanel();
	private JTextField textName;
	private JTextField textLimit;
	private JComboBox comboBox;
	private JTextArea textDesc;
	private int id;
	private String code;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			BusinessTypeAlterDialog dialog = new BusinessTypeAlterDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public BusinessTypeAlterDialog(ManagerUI managerUI,String textName,String textLimit,String comBox,String textDesc,int id) {
		//调用默认构造函数，初始化界面
		this();
		this.managerUI = managerUI;
		this.textName.setText(textName);
		this.textLimit.setText(textLimit);
		this.comboBox.setSelectedItem(comBox);
		this.textDesc.setText(textDesc);
		this.id = id;
		this.code = comBox;
		
		
	}

	/**
	 * Create the dialog.
	 */
	private BusinessTypeAlterDialog() {
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblNewLabel = new JLabel("\u4E1A\u52A1\u4EE3\u7801\uFF1A");
			lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 14));
			lblNewLabel.setBounds(60, 28, 84, 23);
			contentPanel.add(lblNewLabel);
		}
		{
			comboBox = new JComboBox();
			comboBox.setModel(new DefaultComboBoxModel(new String[] {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N"}));
			comboBox.setBounds(147, 28, 145, 23);
			contentPanel.add(comboBox);
		}
		{
			JLabel lblNewLabel = new JLabel("\u4E1A\u52A1\u540D\u79F0\uFF1A");
			lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 14));
			lblNewLabel.setBounds(60, 69, 84, 23);
			contentPanel.add(lblNewLabel);
		}
		{
			JLabel lblNewLabel = new JLabel("\u4E1A\u52A1\u4E0A\u9650\uFF1A");
			lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 14));
			lblNewLabel.setBounds(60, 111, 84, 23);
			contentPanel.add(lblNewLabel);
		}
		{
			JLabel lblNewLabel = new JLabel("\u4E1A\u52A1\u5907\u6CE8\uFF1A");
			lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 14));
			lblNewLabel.setBounds(60, 151, 84, 23);
			contentPanel.add(lblNewLabel);
		}
		{
			textName = new JTextField();
			textName.setBounds(147, 70, 145, 21);
			contentPanel.add(textName);
			textName.setColumns(10);
		}
		{
			textLimit = new JTextField();
			textLimit.setColumns(10);
			textLimit.setBounds(147, 112, 145, 21);
			contentPanel.add(textLimit);
		}
		
		textDesc = new JTextArea();
		textDesc.setBounds(147, 150, 145, 57);
		contentPanel.add(textDesc);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("\u4FDD\u5B58");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						
						BusinessType bType = new BusinessType();
						//获取数据
						String btCode = comboBox.getSelectedItem().toString();
						String btName = textName.getText();
						String btLimit = textLimit.getText();
						String btDesc = textDesc.getText();
			
						if (btName.equals("")) {
							JOptionPane.showMessageDialog(null, "业务名称不能为空");
						}else if (btLimit.equals("")) {
							JOptionPane.showMessageDialog(null, "业务上限不能为空");
						}else if(btCode.equals(code)){
							//保存数据
							bType.setId(id);
							bType.setCode(btCode);
							bType.setName(btName);
							bType.setLimitCount(Integer.parseInt(btLimit));
							bType.setDesc(btDesc);
							bType.setCode(btCode);
							bType.setName(btName);
							bType.setLimitCount(Integer.parseInt(btLimit));
							bType.setDesc(btDesc);
							int ret = businessTypeService.update(bType);
							if (ret > 0) {
								JOptionPane.showMessageDialog(null, "修改成功");
								//刷新父窗口的数据
								BusinessTypeAlterDialog.this.managerUI.flushData();
								//关闭对话框
								BusinessTypeAlterDialog.this.dispose();
							}else {
								JOptionPane.showMessageDialog(null, "修改失败");
							}
						}else {
							//校验
							List<BusinessType> businessTypes = businessTypeService.findAll();
							for (int i = 0; i < businessTypes.size(); i++) {
								if (businessTypes.get(i).getCode().equals(btCode)) {
									JOptionPane.showMessageDialog(null, "业务代号不能重复，请重新选择");
									break;
								}
							}
						}
						
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("\u53D6\u6D88");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						BusinessTypeAlterDialog.this.dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
}
